package info.niwota.dc;

import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

public class DateTimePickerDialog extends AlertDialog implements
		OnClickListener, OnDateChangedListener, OnTimeChangedListener {

	private static final String YEAR = "year";
	private static final String MONTH = "month";
	private static final String DAY = "day";
	private static final String HOUR = "hour";
	private static final String MINUTE = "minute";

	private final DatePicker mDatePicker;
	private final TimePicker mTimePicker;
	private final OnDateSetListener mCallBack;

	private final Calendar mCalendar;

	// private final java.text.DateFormat mTitleDateFormat;

	// private final String[] mWeekDays;

	//private int mInitialYear;
	//private int mInitialMonth;
	//private int mInitialDay;

	public interface OnDateSetListener {
		void onDateSet(DatePicker view, Calendar c);
		void onDateClear();
	}

	public DateTimePickerDialog(Context context, OnDateSetListener callBack,
			Calendar c) {
		super(context);

		mCallBack = callBack;

		// DateFormatSymbols symbols = new DateFormatSymbols();

		// mWeekDays = symbols.getShortWeekdays();

		// mTitleDateFormat = java.text.DateFormat.
		// getDateInstance(java.text.DateFormat.FULL);

		c = (c == null ? getDefaultDateTime() : c);
		mCalendar = c;

		setTitle(R.string.time_alarm);

		// mCalendar = Calendar.getInstance();

		// updateTitle(mInitialYear, mInitialMonth, mInitialDay);

		setButton(context.getText(R.string.action_set), this);
		setButton2(context.getText(R.string.action_clear), this); //(OnClickListener) null
		
		setIcon(R.drawable.ic_dialog_time);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.datetime_picker_dialog, null);

		setView(view);

		mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
		
		final int year = c.get(Calendar.YEAR);
		final int month = c.get(Calendar.MONTH);
		final int day = c.get(Calendar.DAY_OF_MONTH);

		mDatePicker.init(year, month, day, this);
		//
		mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
		mTimePicker.setIs24HourView(true);
		mTimePicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
		mTimePicker.setCurrentMinute(c.get(Calendar.MINUTE));
		mTimePicker.setOnTimeChangedListener(this);
	}

	@Override
	public void show() {
		super.show();

		/*
		 * Sometimes the full month is displayed causing the title to be very
		 * long, in those cases ensure it doesn't wrap to 2 lines (as that looks
		 * jumpy) and ensure we ellipsize the end.
		 */

		// TextView title = (TextView)
		// mDatePicker.findViewById(R.id.alertTitle);
		// title.setSingleLine();
		// title.setEllipsize(TruncateAt.END);
	}

	public void onClick(DialogInterface dialog, int which) {
		if (mCallBack != null) {
			mDatePicker.clearFocus();
			if (which == BUTTON_POSITIVE) {
				mCallBack.onDateSet(mDatePicker, mCalendar);
			} else {
				mCallBack.onDateClear();
			}
		}
	}

	public void onDateChanged(DatePicker view, int year, int month, int day) {
		mCalendar.set(Calendar.YEAR, year);
		mCalendar.set(Calendar.MONTH, month);
		mCalendar.set(Calendar.DAY_OF_MONTH, day);
		//
		// updateTitle(year, month, day);
	}

	public void updateDateTime(Calendar c) {
		if (c == null) {
			c = getDefaultDateTime();
		}
		final int year = c.get(Calendar.YEAR);
		final int month = c.get(Calendar.MONTH);
		final int day = c.get(Calendar.DAY_OF_MONTH);
		mDatePicker.updateDate(year, month, day);
		final int hour = c.get(Calendar.HOUR_OF_DAY);
		final int minute = c.get(Calendar.MINUTE);
		mTimePicker.setCurrentHour(hour);
		mTimePicker.setCurrentMinute(minute);
	}

	private Calendar getDefaultDateTime() {
		Calendar c = java.util.Calendar.getInstance();
		//default to 3 hours from now
		c.setTimeInMillis(System.currentTimeMillis() + (3*60*60*1000));
		return c;
	}

	// private void updateTitle(int year, int month, int day) {
	//        
	// setTitle(mTitleDateFormat.format(mCalendar.getTime()));
	// }

	@Override
	public Bundle onSaveInstanceState() {
		Bundle state = super.onSaveInstanceState();

		state.putInt(YEAR, mDatePicker.getYear());
		state.putInt(MONTH, mDatePicker.getMonth());
		state.putInt(DAY, mDatePicker.getDayOfMonth());
		//
		state.putInt(HOUR, mTimePicker.getCurrentHour());
		state.putInt(MINUTE, mTimePicker.getCurrentMinute());
		//
		return state;
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		//
		int year = savedInstanceState.getInt(YEAR);
		int month = savedInstanceState.getInt(MONTH);
		int day = savedInstanceState.getInt(DAY);
		mDatePicker.init(year, month, day, this);
		//
		int hour = savedInstanceState.getInt(HOUR);
		int minute = savedInstanceState.getInt(MINUTE);
		mTimePicker.setIs24HourView(true);
		mTimePicker.setCurrentHour(hour);
		mTimePicker.setCurrentMinute(minute);
		mTimePicker.setOnTimeChangedListener(this);
		// updateTitle(year, month, day);
	}

	public void onTimeChanged(TimePicker picker, int hour, int minute) {
		mCalendar.set(Calendar.HOUR_OF_DAY, hour);
		mCalendar.set(Calendar.MINUTE, minute);
	}
}
